package com.dji.ux.sample.VirtualSticks;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioGroup;

import com.dji.ux.sample.R;

public class UrgencyLandController {

    private Context mcontext;
    private SharedPreferences landSP;
    static public int urlandSwitch = 0;
    public int urlandPosition = 0;

    public UrgencyLandController(Context context, SharedPreferences sharedPreferences) {
        mcontext = context;
        landSP = sharedPreferences;
        urlandSwitch = landSP.getInt("urlandSwitch", 0);
        urlandPosition = landSP.getInt("urlandPosition", 0);
    }

    public void UrgencyLandConfigDialog() {
        LayoutInflater inflater = LayoutInflater.from(mcontext);
        View vv = inflater.inflate(R.layout.config_urgency_land_dialog, null);

        RadioGroup urSwitch_RG = vv.findViewById(R.id.UrlandSwitch);
        RadioGroup urPosition_RG = vv.findViewById(R.id.UrlandPosition);

        switch (urlandSwitch) {
            case 1:
                urSwitch_RG.check(R.id.UrlandON);
                break;
            case 0:
                urSwitch_RG.check(R.id.UrlandOFF);
                break;
            default:
                urSwitch_RG.check(R.id.UrlandOFF);
                break;
        }

        urSwitch_RG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.UrlandON:
                        urlandSwitch = 1;
                        break;
                    case R.id.UrlandOFF:
                        urlandSwitch = 0;
                        break;
                    default:
                        urlandSwitch = 0;
                        break;

                }
            }
        });

        switch (urlandPosition) {
            case 0:
                urPosition_RG.check(R.id.right);
                break;
            case 1:
                urPosition_RG.check(R.id.down);
                break;
            case 2:
                urPosition_RG.check(R.id.left);
                break;
            case 3:
                urPosition_RG.check(R.id.up);
                break;
            default:
                urPosition_RG.check(R.id.up);
                break;
        }

        urPosition_RG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.right:
                        urlandPosition = 0;
                        break;
                    case R.id.down:
                        urlandPosition = 1;
                        break;
                    case R.id.left:
                        urlandPosition = 2;
                        break;
                    case R.id.up:
                        urlandPosition = 3;
                        break;
                    default:
                        urlandPosition = 0;
                        break;

                }
            }
        });

        new AlertDialog.Builder(mcontext, R.style.jzxz_dialog)
                .setView(vv)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SharedPreferences.Editor editor = landSP.edit();
                        editor.putInt("urlandSwitch", urlandSwitch);
                        editor.putInt("urlandPosition", urlandPosition);
                        editor.apply();
                        dialog.dismiss();
                    }
                })
                .create()
                .show();
    }

}
